/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.service.plugin;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.portal.common.PortalPlugin;
import com.apache.portal.common.annotion.AnntationBean;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * description: 数据字典对接插件
 */
@AnntationBean(name = "dict")
public class DictSystemPluginImpl implements PortalPlugin {

    private Logger log = Logger.getLogger(DictSystemPluginImpl.class);

    public Object doInvoke(HttpServletRequest request, HttpServletResponse response,
            Map<String, String> params) {
        String doCode = params.get("doCode");
        ResultMsg msg = null;
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.putAll(params);
        LoginUser loginUser = PortalPubFactory.getInstance().getLoginUser(request);
        if ("dictCateSave".equals(doCode)) {
            msg = dictCateSave(request, paramsMap);
        } else if ("dictCateInfo".equals(doCode)) {
            return dictCateInfo(request, paramsMap);
        } else if ("dictCateList".equals(doCode)) {
            return dictCateList(request, paramsMap);
        } else if ("dictCateDel".equals(doCode)) {
            return dictCateDel(request, paramsMap);
        } else if ("dictItemList".equals(doCode)) {//以下条目
            return dictItemInfo(request, paramsMap);
        } else if ("dictItemSave".equals(doCode)) {
            paramsMap.put("exeType", "insert");
            return dictItemInfo(request, paramsMap);
        } else if ("dictItemDel".equals(doCode)) {
            paramsMap.put("exeType", "del");
            return dictItemInfo(request, paramsMap);
        } else if ("dictItemView".equals(doCode)) {
            paramsMap.put("exeType", "view");
            return dictItemInfo(request, paramsMap);
        } else if ("initCache".equals(doCode)) {
            paramsMap.put("exeType", "initCache");
            return dictItemInfo(request, paramsMap);
        }
        return msg;
    }

    private ResultMsg dictCateSave(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("dictService", "dictSave", params,
                        PortalPubFactory.getInstance().getRpcInfo("dict"));
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());

            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        } else {
            msg = new ResultMsg("F", entity.getMessage());
        }
        return msg;
    }

    private Object dictCateInfo(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("dictService", "dictInfo", params,
                        PortalPubFactory.getInstance().getRpcInfo("dict"));
        return entity;
    }

    private Object dictCateList(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = (ResultEntity) LoadRpcService.service()
                .doServiceClient("dictService", "dictList", params,
                        PortalPubFactory.getInstance().getRpcInfo("dict"));
        return entity;
    }

    private ResultMsg dictCateDel(HttpServletRequest request, Map<String, Object> params) {
        ResultMsg msg = new ResultMsg("F", "操作失败！");
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("dictService", "dictDel", params,
                        PortalPubFactory.getInstance().getRpcInfo("dict"));
        if (("true".equals(entity.getResult()))) {
            if (!ToolsUtil.isEmpty(entity.getEntity())) {
                msg = new ResultMsg("T", entity.getMessage());
            } else {
                msg = new ResultMsg("F", entity.getMessage());
            }
        } else {
            if (entity != null) {
                msg = new ResultMsg("F", entity.getMessage());
            }
        }
        return msg;
    }

    private Object dictItemInfo(HttpServletRequest request, Map<String, Object> params) {
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient("dictService", "dataItemInfo", params,
                        PortalPubFactory.getInstance().getRpcInfo("dict"));
        return entity;
    }

}
